﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clandestine.Conversations;
using System.IO;
using System.Xml;
using JibberJabber.PartDesigners;

namespace JibberJabber.LivePreviews
{
    public class TempConversation
    {
        private List<SpeakerDesigner> Speakers = new List<SpeakerDesigner>();
        private List<PartDesigner> Content = new List<PartDesigner>();

        private bool needFixupContentUsername;
        private bool contentNoLongerContainsDefault = false;

        public TempConversation()
        {
            SetSpeaker(SampleContent.GetSampleSpeaker());
            SetContent(SampleContent.GetSampleContent());
            needFixupContentUsername = false; // setspeaker will have set this to true!
            contentNoLongerContainsDefault = false; // setcontent
        }

        public void SetSpeaker(SpeakerDesigner speaker)
        {
            Speakers.Clear();
            Speakers.Add(speaker);
            needFixupContentUsername = true;
        }

        public void SetSpeaker(IEnumerable<SpeakerDesigner> speakers)
        {
            this.Speakers.Clear();
            this.Speakers.AddRange(speakers);
            needFixupContentUsername = true;
        }

        public void AddSpeaker(SpeakerDesigner speaker)
        {
            Speakers.Add(speaker);
        }

        public void AddSpeaker(IEnumerable<SpeakerDesigner> speakers)
        {
            Speakers.AddRange(speakers);
        }

        public void SetContent(PartDesigner part)
        {
            Content.Clear();
            Content.Add(part);
            contentNoLongerContainsDefault = true;
        }

        public void SetContent(IEnumerable<PartDesigner> parts)
        {
            Content.Clear();
            Content.AddRange(parts);
            contentNoLongerContainsDefault = true;
        }

        public void AddContent(PartDesigner part)
        {
            Content.Add(part);
        }

        public void AddContent(IEnumerable<PartDesigner> parts)
        {
            Content.AddRange(parts);
        }

        public Conversation ProduceConversation()
        {
            string xml = produceXml();
            return new Conversation(new XmlTextReader(new MemoryStream(
                System.Text.Encoding.UTF8.GetBytes(xml))));
        }

        private string produceXml()
        {
            if (!contentNoLongerContainsDefault && needFixupContentUsername)
                ((TextPartDesigner)Content[0]).cmbSpeaker.Text = Speakers[0].txtName.Text;

            StringBuilder xmlout = new StringBuilder();
            xmlout.AppendLine("<?xml version=\"1.0\" encoding="
                + "\"utf-8\" ?>");
            xmlout.AppendLine("<conversation name=\""
                + System.Guid.NewGuid().ToString() + "\">");
            xmlout.AppendLine("<speakers>");

            // Add the <speaker>s.
            foreach (SpeakerDesigner sd in Speakers)
                xmlout.AppendLine(sd.StartXml + sd.EndXml);

            xmlout.AppendLine("</speakers>");

            // Add other content!
            addXmlRecursive(xmlout, Content);
            
            xmlout.AppendLine("</conversation>");

            return xmlout.ToString();
        }

        private void addXmlRecursive(StringBuilder xmlout, IEnumerable<PartDesigner> tags)
        {
            foreach (PartDesigner pd in tags)
            {
                xmlout.AppendLine(pd.StartXml);
                addXmlRecursive(xmlout, pd.Children);
                xmlout.AppendLine(pd.EndXml);
            }
        }
    }
}
